<?php

$connexio = new mysqli("localhost", "root", "root", "calendaridaw2");

if($connexio->connect_errno > 0) die("Error en la connexio de la BBDD [".$connexio->connect_error."]");

$mail = $_POST['mail']; 
$idevent = $_POST['idevent'];
$resposta = 3;
$rows_resposta = array();
        
$i = 0;
foreach($connexio->query("SELECT mail FROM usuari WHERE mail NOT IN('".$mail."')") as $row ) {
    if($row['mail'] != "admin@gmail.com") {
        $rows_resposta[$i] = $row['mail'];           
        $i++;
    }
}
        
for($j= 0;$j < $i;$j++){
    $mimail = $rows_resposta[$j];
    $rows3 = array();
    $conn_result = $connexio->query("
        SELECT count(aceptat) as total
        FROM invitats
        WHERE '".$mimail."' = mailuser
        and idevent = '".$idevent."'
    ");

    while($rows3[] = $conn_result->fetch_assoc());

    $numero_total = 0;
    foreach ($rows3 as $value) {
        if($value["total"] == '') break;
        $numero_total = $value["total"];
    }
    
    if($numero_total == 0) $rows_resposta[$j] = $rows_resposta[$j]."-2";//El usuari no esta invitat
    else { 
        foreach($connexio->query("SELECT aceptat FROM invitats WHERE '".$mimail."' = mailuser and idevent = '".$idevent."' ") as $row2) {
            $resposta = $row2['aceptat'];
        }
        
        if($resposta == 0)$rows_resposta[$j] = $rows_resposta[$j].'-0'; //usuari no ha acceptat
        if ($resposta == 1 )$rows_resposta[$j] = $rows_resposta[$j].'-1';//usuari ha acceptat 
    }
}

echo json_encode($rows_resposta);
$connexio->close();

?>